package vn.nt.healthcare.server.ws.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import vn.nt.healthcare.server.ws.domain.Company;
import vn.nt.healthcare.server.ws.domain.Location;
import vn.nt.healthcare.server.ws.model.CompanyInfo;
import vn.nt.healthcare.server.ws.repositories.CompanyRepository;
import vn.nt.healthcare.server.ws.service.CompanyService;
import vn.nt.healthcare.server.ws.util.CompanyUtil;

@Service("companyService")
@Transactional
public class CompanyServiceImpl implements CompanyService {

	@Autowired
	private CompanyRepository companyRepository;

	@Override
	public Company getCompanyById(String id) {
		// TODO Auto-generated method stub
		return companyRepository.findOne(id);
	}

	@Override
	public Company getCompanyByCode(String code) {
		// TODO Auto-generated method stub
		return companyRepository.findByCode(code);
	}

	@Override
	public List<CompanyInfo> findAll() {
		// TODO Auto-generated method stub
		List<Company> listCompanies = companyRepository.findAll();
		if (!CollectionUtils.isEmpty(listCompanies)) {
			List<CompanyInfo> infos = new ArrayList<CompanyInfo>();
			for (Company company : listCompanies) {
				List<Location> locations = company.getLocations();
				infos.add(CompanyUtil.initCompanyInfo(locations, company));
			}
			return infos;
		}
		return null;
	}

}
